package com.cneiu.ppms.web.service;

import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.TransParamModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
/**
 * 变压器参数业务
 * @author zhaokf
 * @date 2013-04-20
 */
public class TransParamService extends BaseService
{
	/**
	 * 分页列出变压器参数
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<TransParamModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?0:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 100;
			page = 1;
		}
		String where = "";
		try{
			if (search.get("st_id") != null){
				where = " AND st_id=" + search.get("st_id");
			}
			if (search.get("st_model") != null){
				where = " AND st_model LIKE '%" + search.get("st_model") + "%'";
			}
			where = " WHERE " + where.substring(5);
		}catch (Exception e){
		}
		Page<TransParamModel> paras = null;

		String select_sql = "SELECT * ";
		String query_sql = "FROM SUP_TRAN_STD " + where;

		try{
			paras = TransParamModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return paras;
	}



	/**
	 * 增加变压器参数信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){

		Record record = new Record();
		record.set("st_id", Db.queryBigDecimal("select sup_tran_std_seq.nextval from dual").intValue());
		record.set("st_model", params.get("st_model").toString());
		record.set("st_capacity", params.get("st_capacity").toString());
		boolean isupadded = Db.save("sup_tran_std", "st_id",record);
		if(isupadded){
			return true;
		}
		return setError("你访问的变压器参数信息已存在！");
	}

	/**
	 * 修改变压器参数信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		
		Record record = new Record();
		record.set("st_id", params.get("st_id").toString());
		record.set("st_model", params.get("st_model").toString());
		record.set("st_capacity", params.get("st_capacity").toString());
		boolean isupadded = Db.save("sup_tran_std", "st_id",record);
		if(isupadded){
			return true;
		}
		return setError("您访问的变压器参数信息不存在！");
	}

	/**
	 * 删除变压器参数信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		
		String no = params.get("st_id") == null ? "" : params.get("st_id").toString();
		String sql = "select * from sup_tran_std where st_id=?";
		TransParamModel tm = TransParamModel.dao.findFirst(sql, no);
		if (tm != null)
		{
			tm.delete();
			return true;
		}
		return setError("您访问的变压器参数信息不存在！");
	}
	
	/**
	 * 根据ID查询变压器参数
	 * @param params
	 * @return
	 */
	public static TransParamModel get(Map<String,String> params){
		String no = params.get("st_id")==null?"":params.get("st_id").toString();
		TransParamModel tp = TransParamModel.dao.findFirst("SELECT * FROM SUP_TRAN_STD WHERE st_id=?", no);
		return tp;
	}
}
